//注册

var username = null;
var phone = null;
var password = null;
var rpassword = null;
var saveBtn = null;

$(function() {
	username = $("#username");
	validcode = $("#validcode");
	phone = $("#phone");
	password = $("#password");
	rpassword = $("#rpassword");
	saveBtn = $("#submit");

	username.next().html("*");
	phone.next().html("*");
	password.next().html("*");
	rpassword.next().html("*");
	validcode.next().next().html("*");
	
	username.on("blur", function() {
		
		if(username.val() != null && username.val() != ""){
			$.ajax({
				type : "POST",
				url : "/user/check_username.do",
				data : {username : username.val()},
				dataType : "json",
				success : function(data) {
					if (data.status == 0) { //用户已存在，请重新命名
						validateTip(username.next(), {
							"color" : "red"
						}, imgNo + "*用户已存在，请重新命名", false);
					} else if (data.status == 1) { //用户不存在,可以注册
						validateTip(username.val()!=null && username.next(), {
							"color" : "green"
						}, imgYes, true);
					}
				
				},
				error : function(data) {
					//请求出错
					validateTip(username.next(), {
						"color" : "red"
					}, imgNo + " 请求错误", false);
				}
			});
		}else{
			validateTip(username.next(), {
				"color" : "red"
			}, imgNo + "用户名不能为空", false);
			
		}

	}).on("focus", function() {
		validateTip(username.next(), {
			"color" : "#666666"
		}, "* 请输入用户昵称", false);
	});

	
	phone.on("blur", function() {
		var phone_reg = /^1(3|4|5|7|8)\d{9}$/;
		if (!phone_reg.test(phone.val())) {
			validateTip(phone.next(), {
				"color" : "red"
			}, imgNo + " 手机号码输入不符合规范，请重新输入", false);
		} else {
			validateTip(phone.next().next(), {
				"color" : "green"
			}, imgYes, true);
			//验证手机号是否已注册
			$.ajax({
				type : "POST",
				url : "/user/check_phone.do",
				data : {
					phone : phone.val()
				},
				dataType : "json",
				success : function(data) {
					if (data.status == 0) { //手机号码存在
						validateTip(phone.next(), {
							"color" : "red"
						}, imgNo + "号码已注册", false);
						$('input.sendidcode').attr('disabled', "true");

					} else if (data.status == 1) { //号码不存在
						$('input.sendidcode').removeAttr('disabled');
						validateTip(phone.next(), {
							"color" : "green"
						}, imgYes, true);
					}
				},
				error : function(data) {
					//请求出错
					validateTip(phone.next(), {
						"color" : "red"
					}, imgNo + " 请求错误", false);
				}
			});
		}


	}).on("focus", function() {
		validateTip(phone.next(), {
			"color" : "#666666"
		}, "* 请输入手机号码", false);
	});

	password.on("focus", function() {
		validateTip(password.next(), {
			"color" : "#666666"
		}, "* 密码长度必须是大于6小于20", false);
	}).on("blur", function() {
		if (password.val() != null && password.val().length > 6
			&& password.val().length < 20) {
			validateTip(password.next(), {
				"color" : "green"
			}, imgYes, true);
		} else {
			validateTip(password.next(), {
				"color" : "red"
			}, imgNo + " 密码输入不符合规范，请重新输入", false);
		}
	});


	rpassword.on("focus", function() {
		validateTip(rpassword.next(), {
			"color" : "#666666"
		}, "* 请输入与上面一致的密码", false);
	}).on("blur", function() {
		if (rpassword.val() != null && rpassword.val().length > 6
			&& rpassword.val().length < 20 && password.val() == rpassword.val()) {
			validateTip(rpassword.next(), {
				"color" : "green"
			}, imgYes, true);
		} else {
			validateTip(rpassword.next(), {
				"color" : "red"
			}, imgNo + " 两次密码输入不一致，请重新输入", false);
		}
	});

	//判断验证码是否正确
	validcode.on("focus", function() {
		validateTip(validcode.next().next(), {
			"color" : "red"
		}, " * ", false);
	}).on("blur", function() {
		if (validcode.val() != null && validcode.val().length > 5
			&& validcode.val().length < 7) {
			//判断是否匹配
			$.post("/user/doCheckCode.do", {
				validCode : validcode.val(),
			}, function(data) {
				if (data.status == 0) {
					validateTip(validcode.next().next(), {
						"color" : "green"
					}, imgYes, true);
				} else {
					validateTip(validcode.next().next(), {
						"color" : "red"
					}, imgNo + "验证不匹配", false);
				}
			}, 'json');
		} else {
			validateTip(validcode.next().next(), {
				"color" : "red"
			}, "*", false);
		}
	});



	saveBtn.on("click", function() {
		phone.blur();
		password.blur();
		rpassword.blur();
		validcode.blur();
		
		if (username.attr("validateStatus") == "true"
		    && phone.attr("validateStatus") == "true"
			&& password.attr("validateStatus") == "true") {

			if (confirm("确定提交注册？")) {
				$.ajax({
					type : "POST",
					url : "/user/register.do",
					data : {
						username: username.val(),
						phone : phone.val(),
						password : password.val(),
						validCode : validcode.val()
					},
					dataType : "json",
					success : function(data) {
						if(data.status == 0){
						alert("注册成功");
						window.location.href = "to_tour_login";
						}else{
							alert("验证有误，请仔细检查！");
							window.location.href = "to_tour_register";
						}
					},
					error : function(data) {
						//请求出错
						validateTip(oldpassword.next(), {
							"color" : "red"
						}, imgNo + " 请求错误", false);
					}
				});
			}
		}
	});
});